package com.kolon.business.main.controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.main.dao.MainDao;
import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;
import com.kolon.framework.util.XmlUtil;

@Controller("com.kolon.business.main.controller.XmlMainController")
public class XmlMainController implements SimpleController {
	
	@Resource(name="MainDaoImpl")
	private MainDao mainDao;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/main/xmlMain.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		XmlUtil xml = new XmlUtil();
		Document doc = xml.make("SERV_LIST");
		// 파라미터
		HttpSession httpSession = request.getSession();
		String osNm = ComUtil.getMobileOS(request.getHeader("user-agent"));
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String mdfyDttm = request.getParameter("mdfyDttm");
		userMap.put("mdfyDttm", mdfyDttm);
		userMap.put("servTp", ComUtil.getServTp(osNm));
		// PATH 정보
		String iconUrl = MsgUtil.getMessage("service.icon.url");
		String domainIISurl = MsgUtil.getMessage("domain.iis.url");
		String domainUrl = MsgUtil.getMessage("domain.url");
		String bnerUrl = MsgUtil.getMessage("service.banner.url");		
		// 서비스 목록 정보 조회
		List servList = mainDao.getXmlList(userMap);
		// ELEMENT 생성
		LinkedHashMap<String, Object> dataList = new LinkedHashMap<String, Object>();
		LinkedHashMap<String, Object> userDataList = new LinkedHashMap<String, Object>();
		// 회원정보 ELEMENT
		LinkedHashMap<String, Object> userData = new LinkedHashMap<String, Object>();
		userData.put("CPY_CD", userMap.get("CompanyCD"));
		userData.put("CPY_NM", userMap.get("companyname"));
		userData.put("USER_NM", userMap.get("UserName"));
		userData.put("USER_ID", userMap.get("UserAccount"));
		userData.put("TIT_NM", userMap.get("TitleName"));
		userData.put("DEPT_NM", userMap.get("DeptName"));
		userDataList.put("USER_INFO", userData);
		xml.addElement(doc.getRootElement(), userDataList);
		// BANNER ELEMENT
		List bnerList = mainDao.getXmlBnerList(userMap);
		LinkedHashMap<String, Object> bnerDataList = new LinkedHashMap<String, Object>();
		LinkedHashMap<String, Object> bnerData = new LinkedHashMap<String, Object>();		
		if (bnerList != null && bnerList.size() > 0) {
			Map map = (Map)bnerList.get(0);
			bnerData.put("BNER_NO", map.get("BNER_NO"));
			bnerData.put("BNER_NM", map.get("BNER_NM"));
			bnerData.put("IMG_NM", (map.get("IMG_NM") != null) ? domainUrl + bnerUrl + map.get("IMG_NM") : "");
			bnerData.put("LNK_URL", (map.get("LNK_URL") != null) ? map.get("LNK_URL") : "");
			bnerData.put("USE_YN", map.get("USE_YN"));
			bnerDataList.put("BNER_INFO", bnerData);
			xml.addElement(doc.getRootElement(), bnerDataList);
		}
		// SERVICE ELEMENT
		for (int i=0, size=servList.size(); i<size; i++) {
			LinkedHashMap<String, Object> data = new LinkedHashMap<String, Object>();
			Map map = (Map)servList.get(i);
			if ("콘사이스".equals(map.get("SERV_NM"))) {
				if ("-1".equals(userMap.get("conciseGrade")) || "".equals(userMap.get("conciseGrade"))) {
					continue;
				}
			}
			data.put("SERV_NO", map.get("SERV_NO"));
			data.put("SERV_TP", map.get("SERV_TP"));
			data.put("SERV_NM", map.get("SERV_NM"));
			data.put("IMG_NM", (map.get("IMG_NM") != null) ? domainUrl + iconUrl + map.get("IMG_NM") : "");
			String lnkUrl = ComUtil.oTos(map.get("LNK_URL"));
			if (lnkUrl.indexOf("http") == -1)
				lnkUrl = domainUrl + lnkUrl;
			data.put("LNK_URL", lnkUrl);
			data.put("FRF_TP", map.get("FRF_TP"));
			data.put("FRF_VER", map.get("FRF_VER"));
			data.put("APP_NM", map.get("APP_NM"));
			data.put("RGST_DTTM", map.get("RGST_DTTM"));
			data.put("USE_YN", map.get("USE_YN"));
			data.put("EX_YN", map.get("EX_YN"));
			// 팀 관리 일시
			if (map.get("CPY_CD") != null) {
				if ("N".equals(map.get("T_USE_YN"))) {
					data.put("USE_YN", "N");
					data.put("EX_YN", "N");
				}
			}
			if ("iPhone".equals(map.get("FRF_TP"))) {
			data.put("FILE_S_NM_1", map.get("APP_NM") + "://?" + MsgUtil.getMessage("app.iPhone.url") + domainIISurl + 
					MsgUtil.getMessage("app.file.iPhone.path") + map.get("FDR_NM") + 
					"/" + map.get("FILE_S_NM_1"));
			} else if ("Android".equals(map.get("FRF_TP"))) {
			data.put("FILE_S_NM_1", domainIISurl + 
					MsgUtil.getMessage("app.file.Android.path") + map.get("FDR_NM") + "/" + map.get("FILE_S_NM_1"));
			} else {
			data.put("FILE_S_NM_1", "");
			}
			if (map.get("CPY_CD") == null)
			dataList.put("COMM_SERV_INFO", data);
			else
			dataList.put("CPY_SERV_INFO", data);
			xml.addElement(doc.getRootElement(), dataList);
		}
		
		Map model = new HashMap();
		model.put("xmlDoc", doc);
		// 리턴
		return new ModelAndView("xmlView", model);
	}

}
